﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Economo.Services.DataContext;
using Economo.Services.EntitiesConversion;

namespace Economo.Services.BusinessLogic
{
    public class BankAccountsBusinessLogic
    {
        public List<BusinessEntities.BankAccount> GetBankAccounts()
        {
            BankAccountsDataContext da = new BankAccountsDataContext();
            try
            {
                List<DataContext.BankAccount> accounts = da.GetBankAccounts();
                List<BusinessEntities.BankAccount> result = Convertions.ConvertToBusinessEntity<BusinessEntities.BankAccount, BankAccount>(accounts);
                return result;
            }
            catch (Exception ex)
            {
                ExceptionHandling.ManageExceptions.ManageBusinessLogicException(ex);
                return null;
            }
            finally
            {
                da.Dispose();
            }
        }
        public List<BusinessEntities.BankAccount> GetBankAccounts(byte bankId)
        {
            BankAccountsDataContext da = new BankAccountsDataContext();
            try
            {
                List<DataContext.BankAccount> accounts = da.GetBankAccounts(bankId);
                List<BusinessEntities.BankAccount> result = Convertions.ConvertToBusinessEntity<BusinessEntities.BankAccount,BankAccount>(accounts);                
                return result;
            }
            catch (Exception ex)
            {
                ExceptionHandling.ManageExceptions.ManageBusinessLogicException(ex);
                return null;
            }
            finally
            {
                da.Dispose();
            }
        }

        /// <summary>
        /// Gets an account by BankId and Aaccount Id
        /// </summary>
        /// <param name="bankId"></param>
        /// <param name="accountId"></param>
        /// <returns></returns>
        public BusinessEntities.BankAccount GetAccountById(byte bankId,Int16 accountId)
        {
            BankAccountsDataContext da = new BankAccountsDataContext();
            try
            {
                DataContext.BankAccount account = da.GetAccountById(bankId,accountId);
                BusinessEntities.BankAccount result = Convertions.ConvertToBusinessEntity<BusinessEntities.BankAccount>(account);
                return result;
            }
            catch (Exception ex)
            {
                ExceptionHandling.ManageExceptions.ManageBusinessLogicException(ex);
                return null;
            }
            finally
            {
                da.Dispose();
            }
        }
        /// <summary>
        /// Gets an account by Aaccount Id
        /// </summary>        
        /// <param name="accountId"></param>
        /// <returns></returns>
        public BusinessEntities.BankAccount GetAccountById(Int16 accountId)
        {
            BankAccountsDataContext da = new BankAccountsDataContext();
            try
            {
                DataContext.BankAccount account = da.GetAccountById(accountId);
                BusinessEntities.BankAccount result = Convertions.ConvertToBusinessEntity<BusinessEntities.BankAccount>(account);
                return result;
            }
            catch (Exception ex)
            {
                ExceptionHandling.ManageExceptions.ManageBusinessLogicException(ex);
                return null;
            }
            finally
            {
                da.Dispose();
            }
        }

        public Int16 InsertAccount(BusinessEntities.BankAccount newAccount)
        {
            BankAccountsDataContext da = new BankAccountsDataContext();
            try
            {
                BankAccount account = Convertions.ConvertToDataEntity<BankAccount>(newAccount);
                newAccount.Id = da.Insert(account);
                return newAccount.Id;
            }
            catch (Exception ex)
            {
                ExceptionHandling.ManageExceptions.ManageBusinessLogicException(ex);
                return 0;
            }
            finally
            {
                da.Dispose();
            }
        }

        public void UpdateAccount(BusinessEntities.BankAccount updatedAccount)
        {
            BankAccountsDataContext da = new BankAccountsDataContext();
            try
            {
                BankAccount account = Convertions.ConvertToDataEntity<BankAccount>(updatedAccount);
                da.Update(account);
            }
            catch (Exception ex)
            {
                ExceptionHandling.ManageExceptions.ManageBusinessLogicException(ex);
            }
            finally
            {
                da.Dispose();
            }
        }

        public void DeleteAccount(BusinessEntities.BankAccount accountToDelete)
        {
            BankAccountsDataContext da = new BankAccountsDataContext();
            try
            {
                BankAccount account = Convertions.ConvertToDataEntity<BankAccount>(accountToDelete);
                da.Delete(account);
            }
            catch (Exception ex)
            {
                ExceptionHandling.ManageExceptions.ManageBusinessLogicException(ex);
            }
            finally
            {
                da.Dispose();
            }

        }
        public void DeleteAccount(byte bankId,Int16 accountId)
        {
            BankAccountsDataContext da = new BankAccountsDataContext();
            try
            {
                da.Delete(bankId,accountId);
            }
            catch (Exception ex)
            {
                ExceptionHandling.ManageExceptions.ManageBusinessLogicException(ex);
            }
            finally
            {
                da.Dispose();
            }

        }

    }
}
